<div class="submit-form">
    <p>
        <label for="fileInput">上传文件：</label>
        <input id="fileInput" class="easyui-filebox" name="file" style="width:220px" data-options="buttonText:'选择文件'">
        <a id="uploadButton" href="javascript:" class="easyui-linkbutton" iconCls="iconfont icon-upload">上传</a>
    </p>
    <p style="padding-top:10px">
    <div id="progress" class="easyui-progressbar" style="width:350px;"></div>
    </p>
</div>

<script type="text/javascript">
    var progress = $('#progress');
    var uploadButton = $('#uploadButton');
    var fileInput = $('#fileInput');
    progress.progressbar({text: '未开始：0%', value: 0});
    window._fileUploading = undefined;
    uploadButton.click(function () {
        if (window._fileUploading) {
            var jqxhr = $('#uploadOneDialog').find('form').data('jqxhr');
            if (jqxhr) {
                jqxhr.abort();
            }
            progress.progressbar({text: '上传取消：0%', value: 0});
            window._fileUploading = undefined;
        } else {
            console.log(window._fileUploading);
            fileUploading();
            console.log(window._fileUploading);
        }
        if (window._fileUploading) {
            uploadButton.linkbutton({text: '取消', iconCls: 'iconfont icon-close'});
        } else {
            uploadButton.linkbutton({text: '上传', iconCls: 'iconfont icon-upload'});
        }
    });

    function fileUploading() {
        console.log(1);
        $('#uploadOneDialog').find('form').ajaxSubmit({
            url: Url.System.Api.FILE + '/upload_one',
            type: 'post',
            contentType: false,
            processData: false,
            // contentType: "application/json;charset=utf-8",
            beforeSubmit: function (formData, jqForm, options) {
                console.log(formData);
                console.log(jqForm);
                console.log(options);
                var files = fileInput.filebox('files');
                console.log(files);
                progress.progressbar({text: '未开始：0%', value: 0});
                uploadButton.linkbutton({text: '上传', iconCls: 'iconfont icon-upload'});
                return true;
            },
            uploadProgress: function (event, position, total, percentComplete) {
                progress.progressbar({text: '正在上传...' + percentComplete + '%', value: percentComplete});
                if (percentComplete === 100) {
                    progress.progressbar({text: '正在保存，请稍等...100%', value: 100});
                }
            },
            success: function (res, statusText, xhr, $form) {
                console.log(res);
                if (res && res.code) {
                    progress.progressbar({text: '上传失败：0%', value: 0});
                    $.messager.alert('信息', res.message, 'error');
                } else {
                    if (xhr.readyState === 4) {
                        progress.progressbar({text: '未开始：0%', value: 0});
                        fileInput.filebox('clear');
                        $.messager.show({title: '信息', msg: '上传成功'});
                        $('#file').datagrid('reload');
                    }
                }
                uploadButton.linkbutton({text: '上传', iconCls: 'iconfont icon-upload'});
                window._fileUploading = undefined;
            },
            error: function (xhr) {
                progress.progressbar({text: '上传失败：0%', value: 0});
                if (xhr.responseJSON) {
                    $.messager.alert('信息', xhr.responseJSON.message, 'error');
                }
                uploadButton.linkbutton({text: '上传', iconCls: 'iconfont icon-upload'});
                window._fileUploading = undefined;
            },
        });
        window._fileUploading = true
    }
</script>
